<?php

namespace Home\Controller;
use core\lib\Exception;
use Home\Org\Util\Sqlserver;
use Org\Util\UploadFile;
/**
 * 售前 售后服务
 * @author dem
 *
 */
class CouponController extends HomeController {
    

    

    

    
    
    function initialize() {
        require 'Application/Home/ORG/Util/Sqlserver.class.php';
    }


    function db_cp($tabname){
        return M($tabname,'vrc_','DB_CONFIG_CP');
    }
    


    function couponList(){

        $this->display();
    }

    public function getcouponList()
    {
        $model = $this->db_cp('sap_virtualcard_api');
        $pageIndex = I('pageIndex');
        $pageSize = I('pageSize')?:50;
        $where='1 = 1 ';
        $orderSn= I('orderSn');
        $phone= I('phone');
        if($orderSn){

            $where.= " and orderSn like '%". trim($orderSn) . "%'";
        }
        if($phone){
            $where.= " and phone = '". trim($phone) . "'";
        }

        $all =$model->where($where)->order("id desc")->select();
        $total = count($all);
        $res = $model->where($where)->order("id desc")->limit($pageIndex*$pageSize,$pageSize)->select();
        echo json_encode(array('total'=>$total,'data'=>$res));exit;

    }



    public function editingboc365()
    {
        $result=array('msg'=>'','state'=>200);
        $model =M('boc365Stock');
        $product_id=trim(I('product_id'));
        $data=array(
            'product_name'=>I('product_name'),
            'product_spec'=>I('product_spec'),
            'product_quality'=>I('product_quality'),
            'sale_name'=>I('sale_name'),
            'stock_number'=>I('stock_number'),
            'stock_type'=>trim(I('type')),
            'enable'=>trim(I('enable')),
            'sap_item_code'=>trim(I('sap_item_code')),
        );

       $condition='product_id="'.$product_id.'"';

      $model->where($condition)->save($data);
       $result['msg']="更新成功";
       echo json_encode($result);exit;
    }
    public function resendSms()
    {
        $model = $this->db_cp('sap_virtualcard_api');
        $id=trim(I('id'));
        $data= $model->where("id='".$id."'")->find();
        $this->assign('data',$data);
        $this->display();
    }

    public function resendingdSms()
    {
        $result=array('status'=>200,'msg'=>"发送成功");
        $model = $this->db_cp('sap_virtualcard_api');
        $id=trim(I('id'));
        $phone=trim(I('phone'));
        $data= $model->where("id='".$id."'")->find();
        $msg="您好！您的".$data['goodsName']."兑换码为：".base64_decode($data['cardNo'])."，请妥善保管。如有任何问题请拨打：400-006-6830，祝您生活愉快！";
        $sendres = json_decode($this->_sendSMS($phone, $msg),true);
        $save = ['status'=>0,'sendTime'=>date('Y-m-d H:i:s')];
        if($sendres['ret']==200&&$sendres['data']['code']==0){
            $save['status'] =1 ;
        }
        $model->where("id='".$id."'")->save($save);
        echo json_encode($result);exit;

    }


    /**
     * 调用发送短信接口
     * @param unknown $phone
     * @param unknown $content
     * @return multitype:
     */
    function _sendSMS($phone,$content){
        $api = "http://sms.g-town.com.cn/sms/?service=Message.Send";
        $params = array(
            'phone'  =>$phone,
            'content'=>$content,
            'sysCode' =>'OA'
        );
        return  http($api, $params,'POST');
    }


}